package sso.util;


import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public class WebUtils {
    /**
     * 通过json格式把数据写到客户端
     * @param response
     * @param datamap
     * @throws IOException
     */
    public static  void writeJsonToClient(
            HttpServletResponse response,
            Map<String,Object> datamap) throws IOException {
        //1.设置想要数据的编码

        response.setCharacterEncoding("utf-8");
        //2.告诉浏览器响应数据的内容和编码
        response.setContentType("application/json; charset=utf-8");
        //.3将数据转化为json格式的字符串
        String jsonStr = new ObjectMapper().writeValueAsString(datamap);
        //4.将获取的输出流对象json写到客户端
        PrintWriter out=response.getWriter();
        //通过输出流象客户端输出数据
        out.println(jsonStr);
        out.flush();

    }
}
